package sys

import (
	"go-monitor/agent/g"
	"go-monitor/common/model"
	"go-monitor/common/utility"
	"log"
	"time"
)

//根据配置文件发送心跳
func ReportAgentStatus() {
	if g.Config().Heartbeat.Enabled {
		go reportAgentStatus(time.Duration(g.Config().Heartbeat.Interval) * time.Second)
	}
}

//请求Server发送心跳
func reportAgentStatus(interval time.Duration) {
	for {
		req := model.AgentReportRequest{
			IP:           g.IP(),
			AgentVersion: g.VERSION,
			Status:       1,
			UpdateTime:   utility.GetNowTime(),
		}

		var resp model.SimpleRPCResponse
		err := g.RPCClient.Call("Server.ReportStatus", req, &resp)
		if err != nil || resp.Code != 0 {
			log.Println("call Server.ReportStatus fail:", err, "Request:", req, "Response:", resp)
			g.Logger.Error("call Server.ReportStatus fail:", err, "Request:", req, "Response:", resp)
		}
		time.Sleep(interval)
	}
}
